let App = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    navList: [],
    coupons: {},
    prompt: {
      hidden: !0,
      icon: '../../assets/images/iconfont-order-default.png',
      title: '您还没有相关的优惠券'
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(option) {






    let userId = wx.getStorageSync('userId')
    let navList = [{
        name: '未领取',
        id: '0',
      },
      {
        name: '未使用',
        id: '1',
      }, {
        name: '已使用',
        id: '3',
      }, {
        name: '已过期',
        id: '2',
      }
    ]
    this.setData({
      userId,
      navList,
      activeIndex: this.data.activeIndex,
      'coupons.params.status': parseInt(navList[this.data.activeIndex].id),
    })
    this.onPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  //获取兑换码
  duihuanm: function(e) {
    console.log(e.detail.value)
    this.setData({
      ma: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  duihuan: function() {
    var ma = this.data.ma
    if (ma) {
      console.log("兑换")
      // 兑换
      var params = {
        buyerId: wx.getStorageSync("userId"),
        code: ma
      }
      App.HttpService.duihuan(params).then(res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '领取成功',
            icon: 'success',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'loading',
            duration: 1000
          })
        }
      });
    } else {
      console.log("为空")
      wx.showToast({
        title: '兑换码为空',
        icon: 'loading',
        duration: 1000
      })
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

    wx.showNavigationBarLoading();
    this.initData();
    this.getList(() => {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

    this.getList();

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 初始化数据
  initData() {
    let {
      coupons,
      userId
    } = this.data;
    let params = coupons && coupons.params;
    let status = parseInt(params.status);
    this.setData({
      coupons: {
        items: [],
        total: '',
        params: {
          userId: userId,
          page: 1,
          pageCount: 5,
          status: status,
        }
      }
    })
  },
  // 获取优惠券列表
  getList(cb) {
 
    let coupons = this.data.coupons
    let params = coupons.params
    App.HttpService.couponsLists(params).then(res => {
      cb && cb()
      let data = res.data
      if (data.code == 200) {
        coupons.items = [...coupons.items, ...data.data.couponsList]
        if (data.data.couponsList.length > 0) {
          params.page = params.page + 1
          coupons.total = data.data.counts
        }
        this.setData({
          coupons: coupons,
          'prompt.hidden': coupons.items.length,
        })
      }
    }, e => {
      cb && cb()
    })
  },
  // 切换tabbar
  onTapTag(e) {
    let type = e.currentTarget.dataset.type
    let index = e.currentTarget.dataset.index
    this.initData()
    this.setData({
      activeIndex: index,
      'coupons.params.status': parseInt(type),
    })
    this.getList()
  },
  lingqu: function(e) {
    console.log(e)
    var s = this.data.activeIndex
    if (s == 0) {

      // 请求钱数
      var params = {
        userId: wx.getStorageSync("userId"),
        couponsId: e.currentTarget.dataset.id
      }
      App.HttpService.jhyhq(params).then(res => {

        console.log(res, "数据")
        if (res.data.code == 200)
          wx.showToast({
            title: '领取成功',
            icon: 'success',
            duration: 1000
          })
        this.onLoad();

      });

    } else {
      return
    }

  }
})